# Maintainer: Shiwei Wang <wsw0108@qq.com>

_realname=tree-sitter
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.20.8
pkgrel=1
pkgdesc="An incremental parsing system for programming tools (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://tree-sitter.github.io/"
license=('MIT')
makedepends=("unzip" "${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-rust")
source=("${_realname}-${pkgver}.zip::https://github.com/tree-sitter/tree-sitter/archive/v${pkgver}.zip"
        001-mingw-build.patch)
sha256sums=('5f04d75f2b2b9424131e4b769c1e64f7a82bfcf930c79a87c118d251d44ef6e2'
            '19d2a1608ca5c6138c228b97815c48ac95be2bec1265c207fdc0950a65b8ebad')
noextract=("${_realname}-${pkgver}.zip")

prepare() {
  [[ -d ${_realname}-${pkgver} ]] && rm -rf ${_realname}-${pkgver}
  unzip -q "${_realname}-${pkgver}.zip"
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-mingw-build.patch

  cargo fetch --locked
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  make clean
  make

  cd "${srcdir}/${_realname}-${pkgver}"
  cargo build --locked --offline --release
}

check() {
  cd "${srcdir}/${_realname}-${pkgver}"
  make test
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"
  make PREFIX=${MINGW_PREFIX} DESTDIR="${pkgdir}" install

  install -m755 target/release/tree-sitter.exe "${pkgdir}/${MINGW_PREFIX}/bin"

  install -Dm644 LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
